#!/bin/sh
# Script for capturing testcases for kdenlive_renderer bugs
# Captures:
#   * the temporary still images generated for renderer
#   * the Westley scene list file generated for renderer
#   * renderer process' environment
#   * renderer process' command line
#   * list of renderer process' file descriptors from /proc
#   * all sockets from netstat (for identifying those related to file descriptors above)
#   * all open files from lsof
#   * a script to manually launch kdenlive_renderer in analogous configuration
#
# Usage:
#	Change DEST_DIR in source code to the build directory of KDEnlive version your're running.
# Start export from KDEnlive, then:
#   cd SOME_DIRECTORY_TO_PLACE_TESTCASE_AND_DEBUG_FILES
# 	capture_kdenlive_renderer_testcase.sh
#
# Aleksander Adamowski (Olo)
# $Id: capture_kdenlive_renderer_testcase.sh 27 2007-07-12 21:06:41Z aleksander.adamowski $

export DEST_DIR=~/build/kdenlive

tstamp=$(date +%F_%H_%M_%S)
uid=$(id -un)
renderer_pid=$(ps xuwa | grep kdenlive_renderer | grep '[/]tmp' | awk '{print $2}')
outfile="$PWD/${tstamp}_untitled-test_run.vob"
renderer_args="$(ps xuwa | grep kdenlive_renderer | grep '[/]tmp' | perl -pe 's/^(\S+\s+){12}//;' | perl -pe "s{(-consumer)\s+(\S+)\s+(\S+)}{\$1 \$2:$outfile};")"
renderer_projectfile=/tmp/kde-$uid/$(ps xuwa | grep kdenlive_renderer | grep -o  'kdenlive[a-zA-Z0-9]*.westley')


ls -l /proc/$renderer_pid/ > ${tstamp}_ls_proc.txt
cp $renderer_projectfile ./$tstamp.westley
cp /proc/$renderer_pid/environ ./$tstamp.environ
cp /proc/$renderer_pid/cmdline ./$tstamp.cmdline
ls -l /proc/$renderer_pid/fd > ./$tstamp.fd
netstat -anp > ./$tstamp.netstat
lsof -n > ./$tstamp.lsof
for pngfile in $(grep -o '[a-zA-Z0-9]*.png' $tstamp.westley); do
	cp /tmp/kde-$uid/kdenlive/$pngfile ./
	perl -pe "s{/tmp/kde-$uid/kdenlive/$pngfile}{$PWD/$pngfile};" -i $tstamp.westley
done

export tstamp
(
echo "#!/bin/sh";
echo "export PATH=\"$DEST_DIR/bin:$PATH\""
echo "export LD_LIBRARY_PATH=$DEST_DIR/lib"
echo "$DEST_DIR/bin/kdenlive_renderer $PWD/$tstamp.westley $renderer_args"
) > ${tstamp}_test_run.sh

